import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from "../views/Home";
import Login from "../views/Login";
import Index from "../views/Index";
import User from "../views/System/User";
import Article from "../views/System/Article";
import UserCenter from "../views/UserCenter";
import ArticleData from "../views/Data/ArticleData";

Vue.use(VueRouter)

const routes = [
  {
    path: '/admin',
    name: 'Home',
    component: Home,
    children: [
      {
        path: 'index',
        name: 'Index',
        component: Index,
        meta: {
          title: '首页'
        }
      },
      {
        path: 'usercenter',
        name: 'UserCenter',
        component: UserCenter,
        meta: {
          title: '用户中心'
        }
      },
      {
        path: 'system/user',
        name: 'User',
        component: User,
        meta: {
          title: '用户菜单'
        }
      },
      {
        path: 'system/article',
        name: 'Article',
        component: Article,
        meta: {
          title: '文章菜单'
        }
      },
      {
        path: 'data/article',
        name: 'ArticleData',
        component: ArticleData,
        meta: {
          title: '标签数据'
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/',
    redirect: '/login'
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  let token = localStorage.getItem('token')
  console.log(token)
  if(to.path === '/login')
    next()
  else if(!token || token === '')
    next({path: '/login'})
  else
    next()
})

export default router
